/* 
<table class="layui-hide" id="test" lay-filter="test"></table>
layui.use(['table'],function () {
  var table = layui.table;
  table.render({
    elem: '#test'//为此id
  })
}) 
*/
// note: 此函数需要放到layui内部
/*
 * {String}  id -表格所绑定的元素id
 * {Object}  option -默认值{hasTool: true,hasPage: true}，
 * # hasTool,数据表格是否有头部工具栏, # hasPage，数据表格是否有分页
 */
function dblclickTableHeader(id, option) {
  let default_option = {
    hasTool: true,
    hasPage: true,
  };
  option = Object.assign({},default_option, option);

  // 判断是否选中元素
  if (!$(id)) {
    console.log("%c 无选中元素"," color:red;font-size:18px;");
    return;
  }
  var _divChildren = $(id).next().children();

  // 判断是否符合规则，防止js报错 
  if (option.hasTool && option.hasPage && _divChildren.length !== 4) {
    console.log("%c hasTool 与 hasPage 都为 TRUE，而表格的子元素长度不为4"," color:red;font-size:18px;");
    return;
  } else if(!option.hasTool && option.hasPage && _divChildren.length !== 3){
    console.log("%c hasTool 为 FALSE , hasPage 为 TRUE，而表格的子元素长度不为3"," color:red;font-size:18px;");
    return;
  } else if(option.hasTool && !option.hasPage && _divChildren.length !== 3){
    console.log("%c hasTool 为 TRUE , hasPage 为 FALSE ，而表格的子元素长度不为3"," color:red;font-size:18px;");
    return;
  } else if(!option.hasTool && !option.hasPage && _divChildren.length !== 2){
    console.log("%c hasTool 与 hasPage 都为 FALSE ，而表格的子元素长度不为2"," color:red;font-size:18px;");
    return;
  }

  var layuiTableHeader, layuiTableStyle, layuiTableStyleText;

  layuiTableHeader = option.hasTool
    ? _divChildren.eq(1).find(".layui-table-header").eq(0)
    : _divChildren.eq(0).find(".layui-table-header").eq(0);

  layuiTableStyle = option.hasTool
    ? option.hasPage
      ? _divChildren.eq(3)[0]
      : _divChildren.eq(2)[0]
    : option.hasPage
    ? _divChildren.eq(2)[0]
    : _divChildren.eq(1)[0];
  
  layuiTableStyleText = layuiTableStyle.innerHTML.replace(/\s/g, "");

  // console.log(layuiTableStyleText);
  
  // 设置双击表头展开，设置延时等待表格加载完成
  setTimeout(function () {
    layuiTableHeader.find("th").each((index, element) => {
      var key = $(element).attr("data-key");
      let Reg1 = new RegExp("(" + key + ")" + "{}", "g");
      var _width = $(element).width();

      if (layuiTableStyleText.match(Reg1) && _width > 0) {
        let _result = layuiTableStyleText.match(Reg1)[0];
        layuiTableStyleText = layuiTableStyleText.replace(
          _result,
          key + "{width:" + _width + "px;}"
        );
        if (layuiTableHeader.find("th").length == index - 1) {
          layuiTableStyle.innerHTML = layuiTableStyleText;
        }
      }
    });

    layuiTableHeader.find("th").each((index, element) => {
      var key = $(element).attr("data-key");
      $(element).on("dblclick", function () {
        var elClassName = ".laytable-cell-" + key;
        var $div = $(
          [
            "<div id='test_len' ",
            "style='white-space: nowrap;visibility: hidden;position:absolute;left:0;top:0;'>",
            "</div>",
          ].join("")
        );

        $(document.body).append($div);

        var width = 0;

        $(elClassName).each((i, el) => {
          let padding_left = $(el).css("padding-left");
          let padding_right = $(el).css("padding-right");
          let text = $(el).html();
          $("#test_len").html(text);
          $("#test_len").css({
            "padding-left": padding_left,
            "padding-right": padding_right,
          });

          width =
            width > $("#test_len").innerWidth()
              ? width
              : $("#test_len").innerWidth();
        });

        width += "px";

        $div.remove();

        let Reg2 = new RegExp(
          "(" +
            key +
            "\\{width:)\\d*\\.?\\d*(px|rem|%|vw|em|mm|in|cm|pt);?(.*?\\})"
        );

        layuiTableStyleText = layuiTableStyleText.replace(Reg2, function (
          $0,
          $1,
          $2,
          $3,
          $4
        ) {
          return $1 + width + ";" + $3;
        });

        layuiTableStyle.innerHTML = layuiTableStyleText;
      });
    });
  }, 300);
}
